using Aurora.Redis.CSRedis;
using Aurora.Redis.NewLifeRedis;
using Aurora.Redis.ServiceStackRedis;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Aurora.Redis;

public static class RedisServiceExtension
{
    public static void AddRedis(this IServiceCollection services, IConfiguration configuration)
    {
        var usage = configuration["RedisUsage"]!;
        if (usage.Equals("ServiceStack"))
        {
            var config = configuration.GetSection("ServiceStack").Get<ServiceStackRedisConfig>();
            ServiceStackRedisFactory.ConfigRedisNode(config);
            services.AddScoped<IRedisCache, ServiceStackRedisCache>();
            services.AddSingleton<IDistributedCache, ServiceStackRedisCache>();
        }
        else if (usage.Equals("NewLife"))
        {
            services.AddSingleton<NewLifeRedisFactory>(p => new NewLifeRedisFactory(configuration["NewLife.Redis"]));
            services.AddScoped<IRedisCache, NewLifeRedisCache>();
            services.AddSingleton<IDistributedCache, NewLifeRedisCache>();
        }
        else
        {
            services.AddSingleton<CSRedisFactory>(p => new CSRedisFactory(configuration["CSRedis"]));
            services.AddScoped<IRedisCache, CSRedisCache>();
            services.AddSingleton<IDistributedCache, CSRedisCache>();
        }
    }
}